<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of paypalclass
 *
 * @author Administrator
 */
class paypalclass {
    //put your code here
    private $_params;
    private $url; //  这是，你必须提交您的API请求连接服务器的URL。
    private $payurl;//支付地址
    public function __construct() {
        $this->url="https://api-3t.sandbox.paypal.com/nvp";
        $this->payurl="https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=";
    }
     public function FirstPay($data){
       if(!empty($this->_params->Username)&&!empty($this->_params->Password)&&!empty($this->_params->Signature)){
           $post=  $this->getHeader();
           $post["METHOD"]="SetExpressCheckout";
           $deposit_arg="";
           if(isset($data["deposit"])&&$data["deposit"]>0){
               $deposit_arg="&deposit_pay_status=1";
           }
           $post["RETURNURL"]=urlencode("http://101.200.73.63/PayApi/callback?order_no=".$_SESSION["OrderNo"]."&paytype=paypal{$deposit_arg}&rentid={$data["id"]}&rentiid={$data["itid"]}");
           $post["CANCELURL"]=urlencode("http://101.200.73.63/PayApi/cancelurl?order_no=".$_SESSION["OrderNo"]."&paytype=paypal&rentid={$data["id"]}");
           $post["L_PAYMENTREQUEST_0_NAME0"]=$data["year"]."年".$data["month"]."月房租";
           $post["L_PAYMENTREQUEST_0_AMT0"]=(string)round($data["l_rent"], 2);
           $post["L_PAYMENTREQUEST_0_QTY0"]= 1;
           $i=1;
           if(isset($data["l_water_price"])&&$data["l_water_price"]>0){
               $post["L_PAYMENTREQUEST_0_NAME{$i}"]=$data["year"]."年".$data["month"]."月水费";
               $post["L_PAYMENTREQUEST_0_AMT{$i}"]=(string)round($data["l_water_price"], 2);
               $post["L_PAYMENTREQUEST_0_QTY{$i}"]= 1;
               $i=$i+1;
           }
           if(isset($data["l_dian_price"])&&$data["l_dian_price"]>0){
               $post["L_PAYMENTREQUEST_0_NAME{$i}"]=$data["year"]."年".$data["month"]."月电费";
               $post["L_PAYMENTREQUEST_0_AMT{$i}"]=(string)round($data["l_dian_price"], 2);
               $post["L_PAYMENTREQUEST_0_QTY{$i}"]= 1;
               $i=$i+1;
           }
           if(isset($data["deposit"])&&$data["deposit"]>0){
               $post["L_PAYMENTREQUEST_0_NAME{$i}"]="租房押金";
               $post["L_PAYMENTREQUEST_0_AMT{$i}"]=(string)round($data["deposit"], 2);
               $post["L_PAYMENTREQUEST_0_QTY{$i}"]= 1;
               $data["l_total"]=$data["l_total"]+$data["deposit"];
           }
           $post["PAYMENTREQUEST_0_ITEMAMT"]=  (string)round($data["l_total"],2);
           $post["PAYMENTREQUEST_0_AMT"]=(string)round($data["l_total"],2);
           $post["PAYMENTREQUEST_0_PAYMENTACTION"]="Sale";
           //$post["PAYMENTREQUEST_0_CURRENCYCODE"]="RUB";
      
           $content=  Common::Curl_Post($this->url, $this->setarraykv($post));
           parse_str($content,$response);
           if(strtolower($response["ACK"])=="success"){
                 $_SESSION["token"]=$response["TOKEN"];
                 $payPalURL = $this->payurl.urldecode($response["TOKEN"]);
                 header("Location: ".$payPalURL);
           }else{
               echo $content;
               //Common::showmessage("贝宝响应错误", "错误信息",Common::RewriteUrl("Tenantadmin","canrentlist"));
           }
           
           
       }else{
           Common::showmessage("贝宝配置有误", "错误信息",Common::RewriteUrl("Tenantadmin","canrentlist"));
       }
       
    }
    
   
    public function getUserInfo($token){
        $post=  $this->getHeader();
        $post["METHOD"]="GetExpressCheckoutDetails";
        $post["TOKEN"]=$token;
        $content=Common::Curl_Post($this->url, $this->setarraykv($post));
        parse_str($content,$response);
        return $response;
    }
    public function doPay($token,$payerid,$amt){
        $post=  $this->getHeader(); 
        $post["METHOD"]="DoExpressCheckoutPayment";
        $post["TOKEN"]=$token;
        $post["PAYERID"]=$payerid;
        $post["PAYMENTREQUEST_0_PAYMENTACTION"]="Sale";
        $post["PAYMENTREQUEST_0_AMT"]=(string)round($amt, 2);
        //$post["PAYMENTREQUEST_0_CURRENCYCODE"]="RUB";
        $content=Common::Curl_Post($this->url, $this->setarraykv($post));
        parse_str($content,$response);
        return $response;
    }

    private function getHeader(){
        $post=array();
        $post["VERSION"]="109.0";
        $post["USER"]=$this->_params->Username;
        $post["PWD"]=$this->_params->Password;
        $post["SIGNATURE"]=$this->_params->Signature;
        $post["LOCALECODE"]="zh_CN";
        return $post;
    }
    
    private function setarraykv($arr)
  {
    $cache = '';
    $i=0;
    foreach ($arr as $k => $v)
    {
      if($i==0){
          $cache.=$k . '=' . $v;
      }else{
        $cache.='&' . $k . '=' . $v;
      }
      $i=$i+1;
    }
    return $cache;
  }
     public function SetParams($params){
        $this->_params=  json_decode($params);
    }
}
